//
//  THAlertContentView.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THAlertContentView.h"

@interface THAlertContentView ()

@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *descLabel;
@property (nonatomic, strong)UIButton *cancelBtn;
@property (nonatomic, strong)UIButton *sureBtn;

@property (nonatomic, strong)UIView   *selectBgView;
@property (nonatomic, strong)UIImageView *selectImgV;
@property (nonatomic, strong)UILabel  *selectDescLabel;
@property (nonatomic, assign)BOOL isSelect;

@end

@implementation THAlertContentView

#pragma mark - 子视图配置
/// 配置子控件
- (void)configViews
{
    UIImageView *bgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"baseImg_alert_bg_img"]];
    [self addSubview:bgV];
    [bgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(0);
        make.height.equalTo(kSizeRateTo(120));
    }];
    
    
    UIView *lastView;
    
    if (self.title.length > 0) {
        [self addSubview:self.titleLabel];
        self.titleLabel.text = self.title;
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(kSizeRateTo(20));
            make.left.greaterThanOrEqualTo(kSizeRateTo(20));
            make.centerX.equalTo(self);
            make.height.equalTo(kSizeRateTo(25));
        }];
    }
    

    [self addSubview:self.descLabel];
    self.descLabel.text = self.desc;
    
    CGFloat bottomSpace = kSizeRateTo(86);
    
    if (self.btnVerticalDirection && self.cancelBtnTitle.length>0 && self.sureBtnTitle.length>0) {
        bottomSpace = kSizeRateTo(118);
    }
    
    CGFloat height = [self.desc boundingRectWithSize:CGSizeMake(kSizeRateTo(240), MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:kFontRegular(kSizeRateTo(15))} context:nil].size.height+1;
    
    if(self.descAttr.length > 0){
        self.descLabel.attributedText = self.descAttr;
        
        height = [self.descAttr boundingRectWithSize:CGSizeMake(kSizeRateTo(240), MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) context:nil].size.height+1;
    }
    
    
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kSizeRateTo((self.title.length > 0)?55:25));
        make.left.greaterThanOrEqualTo(kSizeRateTo(20));
        make.centerX.equalTo(0);
        make.bottom.equalTo(-bottomSpace);
        make.height.equalTo(height);
    }];
    lastView = self.descLabel;
//    if (self.selectTitle) {
//        self.isSelect = YES;
//        self.selectDescLabel.text = self.selectTitle;
//        
//        [self addSubview:self.selectBgView];
//        [self.selectBgView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.descLabel.mas_bottom).offset(6);
//            make.centerX.equalTo(self);
//            make.height.mas_equalTo(26);
//        }];
//        [self.selectBgView addSubview:self.selectImgV];
//        [self.selectImgV mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.mas_equalTo(CGSizeMake(16, 16));
//            make.centerY.equalTo(self.selectBgView);
//            make.left.equalTo(self.selectBgView);
//        }];
//        [self.selectBgView addSubview:self.selectDescLabel];
//        [self.selectDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.selectImgV.mas_right).offset(5);
//            make.right.equalTo(self.selectBgView);
//            make.centerY.equalTo(self.selectBgView);
//        }];
//        lastView = self.selectBgView;
//    }
    if (self.cancelBtnTitle.length > 0) {
        [self.sureBtn setTitle:self.sureBtnTitle forState:UIControlStateNormal];
        [self.cancelBtn setTitle:self.cancelBtnTitle forState:UIControlStateNormal];
        
        [self addSubview:self.cancelBtn];
        [self addSubview:self.sureBtn];
        
        
        if (self.btnVerticalDirection) {
            self.cancelBtn.titleLabel.font = kFontRegular(kSizeRateTo(14));
            
            [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(0);
                make.left.equalTo(kSizeRateTo(20));
                make.bottom.equalTo(-kSizeRateTo(20));
                make.height.equalTo(kSizeRateTo(20));
            }];
            [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(0);
                make.left.equalTo(kSizeRateTo(20));
                make.bottom.equalTo(-kSizeRateTo(57));
                make.height.equalTo(kSizeRateTo(46));
            }];
        }else{
            [_cancelBtn zh_roundedCornerWithRadius:kSizeRateTo(23) borderColor:kThemeColor borderWidth:1];
            [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(kSizeRateTo(114), kSizeRateTo(46)));
                make.left.equalTo(kSizeRateTo(20));
                make.bottom.equalTo(-kSizeRateTo(20));
            }];
            [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(kSizeRateTo(114), kSizeRateTo(46)));
                make.right.equalTo(-kSizeRateTo(20));
                make.bottom.equalTo(-kSizeRateTo(20));
            }];
        }
    }else{
        [self.sureBtn setTitle:self.sureBtnTitle forState:UIControlStateNormal];
        [self addSubview:self.sureBtn];

        [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(kSizeRateTo(240), kSizeRateTo(46)));
            make.centerX.equalTo(0);
            make.bottom.equalTo(-kSizeRateTo(20));
        }];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [[THCurrentVCWindowsManager shareManager].currentWindow endEditing:YES];
}

#pragma mark - btn event

- (void)onCancelBtnClick:(UIButton *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(zh_alertContentViewDelegate_cancelEvent)]) {
        [self.delegate zh_alertContentViewDelegate_cancelEvent];
    }
}

- (void)onSureBtnClick:(UIButton *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(zh_alertContentViewDelegate_sureEvent)]) {
        [self.delegate zh_alertContentViewDelegate_sureEvent];
    }
}

- (void)onSelectBgViewTap:(UITapGestureRecognizer *)tap
{
    self.isSelect = !self.isSelect;
    self.selectImgV.image = self.isSelect == YES ? kImage(@"baseImg_common_checkMark_s") : kImage(@"baseImg_common_checkMark_n");
}

#pragma mark - lazy

- (UILabel *)titleLabel
{
    if(!_titleLabel)
    {
        _titleLabel=[[UILabel alloc]initWithFontName:FONT_MEDIUM size:kSizeRateTo(18) textColor:Color_text_33];
        _titleLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _titleLabel;
}

- (UILabel *)descLabel
{
    if(!_descLabel)
    {
        _descLabel= [[UILabel alloc]initWithFontName:FONT_REGULAR size:kSizeRateTo(15) textColor:Color_text_33 textAlignment:NSTextAlignmentCenter];
        _descLabel.numberOfLines = 0;
    }
    return _descLabel;
}

- (UIButton *)cancelBtn
{
    if(!_cancelBtn)
    {
        _cancelBtn=[[UIButton alloc]init];
        _cancelBtn.titleLabel.font = kFontMedium(kSizeRateTo(16));
        [_cancelBtn setTitleColor:kThemeColor forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(onCancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//        [_cancelBtn setBackgroundColor:ColorFromHex(0xDDDDDD)];
    }
    return _cancelBtn;
}

- (UIButton *)sureBtn
{
    if(!_sureBtn)
    {
        _sureBtn=[[UIButton alloc]init];
        [_sureBtn zh_roundedCornerWithRadius:kSizeRateTo(23)];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = kFontMedium(kSizeRateTo(16));
        [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _sureBtn.backgroundColor = kThemeColor;
        [_sureBtn addTarget:self action:@selector(onSureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

- (UIView *)selectBgView
{
    if(!_selectBgView)
    {
        _selectBgView=[[UIView alloc]init];
        [_selectBgView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onSelectBgViewTap:)]];
    }
    return _selectBgView;
}

- (UIImageView *)selectImgV
{
    if(!_selectImgV)
    {
        _selectImgV=[[UIImageView alloc]init];
        _selectImgV.image = kImage(@"baseImg_common_checkMark_s");
    }
    return _selectImgV;
}

- (UILabel *)selectDescLabel
{
    if(!_selectDescLabel)
    {
        _selectDescLabel=[[UILabel alloc]initWithFontName:FONT_REGULAR size:kSizeRateTo(12) textColor:Color_text_99];
    }
    return _selectDescLabel;
}


@end
